$('#link_reg').click(function () {
    $('.login').hide()
    $('.reg').show()
})
$('#link_login').click(function () {
    $('.reg').hide()
    $('.login').show()
})

let form = layui.form;
let layer = layui.layer;
form.verify({
    pwd: [
        /^[\S]{6,12}$/
        , '密码必须6到12位，且不能出现空格'
    ],
    repwd: function (value) {
        if (value !== $('.reg [type=password]').val()) return '两次密码输入不一致'
    }
})

$('#reg_from').submit((e) => {
    e.preventDefault()
    $.post('/api/reguser',
        {
            username: $('#reg_from [name="username"]').val(),
            password: $('#reg_from [name="password"]').val()
        },
        (res) => {
            if (res.status !== 0) {
                return layer.msg(res.message)
            }
            layer.msg(res.message)
            $('#link_login').click()
        }
    )
})

$('#login_from').submit(function (e) {
    e.preventDefault()
    $.post('/api/login',
        $(this).serialize(),
        (res) => {
            if (res.status !== 0) return layer.msg(res.message)
            localStorage.setItem('token', res.token)
            location.href = './index.html'
        }
    )
})